package date;

import org.junit.Test;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Calendar;

/**
 * JDK8 API
 * LocalDate / LocalTime / LocalDateTime
 * Instant
 * DateTimeFormatter ---> 类似于 SimpleDateFormatter
 */
public class After8DateTest {
    @Test
    public void test1() {
        /*
        now():获取当前日期和时间对应的实例
        2025-03-05
        12:01:03.115046800
        2025-03-05T12:01:03.115046800
         */
        System.out.println(LocalDate.now());
        System.out.println(LocalTime.now());
        System.out.println(LocalDateTime.now());

        /*
         * of():获取指定日期/时间对应的实例
         * 2000-11-16
         * 2000-11-16T08:31
         */
        System.out.println(
                LocalDate.of(2000, 11, 16));
        System.out.println(
                LocalDateTime.of(2000, 11, 16, 8, 31));


        /*
         * Instant:瞬时--->类似于Date
         */
        Instant instant = Instant.now();
        System.out.println(instant); // 2025-03-05T04:13:48.789529600Z
        OffsetDateTime instant1 = instant.atOffset(ZoneOffset.ofHours(8));
        System.out.println(instant1); // 2025-03-05T12:15:25.991890200+08:00
    }

    @Test
    public void testDateTimeFormatter() {
        // 自定义的格式. ofPattern("yyyy-MM-dd hh:mm:ss")
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

        LocalDateTime now = LocalDateTime.now();
        String formatted = dateTimeFormatter.format(now);
        System.out.println(formatted);

        TemporalAccessor parse = dateTimeFormatter.parse(formatted);
        LocalDateTime localDateTime = LocalDateTime.from(parse);
        System.out.println(localDateTime);
    }

    @Test
    public void testExr() {
        Calendar calendarBirth = Calendar.getInstance();
//        System.out.println(calendarBirth.getTime());
        System.out.println(calendarBirth.get(Calendar.MONTH) + 1 + "月" + calendarBirth.get(Calendar.DAY_OF_MONTH) + "日");

        calendarBirth.add(Calendar.DAY_OF_MONTH, 100);
        System.out.println(calendarBirth.get(Calendar.MONTH) + 1 + "月" + calendarBirth.get(Calendar.DAY_OF_MONTH) + "日");

        LocalDateTime localDateTime = LocalDateTime.now();
        System.out.println(localDateTime.getMonthValue() + "月" + localDateTime.getDayOfMonth() + "日");
        LocalDateTime localDateTimeBirth = localDateTime.plusDays(100);
        System.out.println(localDateTimeBirth.getMonthValue() + "月" + localDateTimeBirth.getDayOfMonth() + "日");

    }
}
